package com.skcc.ngms.matest;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.skcc.alopex.common.activity.MainActivity;

public class C2dm_BroadcastReceiver extends BroadcastReceiver{
    
	
	static String registration_id = null;
	static String c2dm_msg = "";
	private NotificationManager nm;
	private MainActivity mActivity = MainActivity.mInstance;
	
    @Override
    public void onReceive(Context context, Intent intent) {
    	if (intent.getAction().equals("com.google.android.c2dm.intent.REGISTRATION")) {
    		
    		handleRegistration(context, intent);
    		
    	} else if (intent.getAction().equals("com.google.android.c2dm.intent.RECEIVE")) {
    		
    		c2dm_msg = intent.getExtras().getString("msg");
    		String[] splitedMsg = c2dm_msg.split("&");
    		
    		Map<String, String> msgMap = new HashMap<String, String>();
    		
    		for(String msg : splitedMsg){
    			String key = msg.substring(0, msg.indexOf("="));
    			String value = msg.substring(msg.indexOf("=")+1, msg.length());
    			msgMap.put(key, value);
    		}
    		System.out.println("c2dm_msg======>"+c2dm_msg);
    		
    		nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    		
			System.out.println("yong activity check: "+mActivity);
			
    		Intent intentMain = new Intent(Intent.ACTION_MAIN);
    		intentMain.addCategory(Intent.CATEGORY_LAUNCHER);
    		intentMain.setComponent(new ComponentName(context, NotificationMessage.class));
//			Intent intentMain = null;
//			if(mActivity == null){
//				System.out.println("1");
//				intentMain = new Intent(context, NotificationMessageNewActivity.class);
//			}else{
//				System.out.println("2");
//				intentMain = new Intent(context, NotificationMessage.class);
//			}
//   		 	intentMain.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    		
    		Iterator<String> iter = msgMap.keySet().iterator();
    		String key = null;
    		while(iter.hasNext()){
    			key = iter.next();
    			intentMain.putExtra(key, msgMap.get(key));
    		}
    		
    		PendingIntent intent2 = PendingIntent.getActivity(context, 0, intentMain, PendingIntent.FLAG_UPDATE_CURRENT);
    		
			  
//    		Intent intentMain = new Intent(Intent.ACTION_MAIN);
//    		intentMain.addCategory(Intent.CATEGORY_LAUNCHER);
//    		intentMain.setComponent(new ComponentName(context, C2dmTest.class));
//    		PendingIntent intent2 = PendingIntent.getActivity(context, 0, intentMain, 0);
										
			// Create Notification Object
			Notification notification =
				new Notification(android.R.drawable.ic_input_add, msgMap.get("title"), System.currentTimeMillis());
			notification.setLatestEventInfo(context, msgMap.get("title"), msgMap.get("body"), intent2);
			
			nm.notify(1234, notification);
    		
    	}
    }
    
    private void handleRegistration(Context context, Intent intent) {
    	
    	registration_id = intent.getStringExtra("registration_id");
    	
    	System.out.println("registration_id====>"+registration_id);
    	
    	if (intent.getStringExtra("error") != null) {
    	
    		Log.v("C2DM_REGISTRATION",">>>>>" + "Registration failed, should try again later." + "<<<<<");
    	
    	} else if (intent.getStringExtra("unregistered") != null) {

    		Log.v("C2DM_REGISTRATION",">>>>>" + "unregistration done, new messages from the authorized sender will be rejected" + "<<<<<");
    	
    	} else if (registration_id != null) {
    		
    		System.out.println("registration_id complete!!");
    	}
    }
    
    
}